﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using HumanManagement.BusinessObjects;

namespace HumanManagement.DataAccess
{
	public class SubFunctionDA
	{

		#region ***** Init Methods ***** 
		public SubFunctionDA()
		{
		}
		#endregion

		#region ***** Get Methods ***** 
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public SubFunction Populate(IDataReader myReader)
		{
			SubFunction obj = new SubFunction();
			obj.ID = (int) myReader["ID"];
			obj.FunctionID = (int) myReader["FunctionID"];
			obj.SubFunctionName = (string) myReader["SubFunctionName"];
			obj.Used = (string) myReader["Used"];
			return obj;
		}

		/// <summary>
		/// Get SubFunction by id
		/// </summary>
		/// <param name="id">ID</param>
		/// <returns>SubFunction</returns>
		public SubFunction GetByID(int id)
		{
			using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_SubFunction_GetByID", Data.CreateParameter("ID", id)))
			{
				if (reader.Read())
				{
					return Populate(reader);
				}
				return null;
			}
		}

		/// <summary>
		/// Get all of SubFunction
		/// </summary>
		/// <returns>List<<SubFunction>></returns>
		public List<SubFunction> GetList()
		{
			using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_SubFunction_Get"))
			{
				List<SubFunction> list = new List<SubFunction>();
				while (reader.Read())
				{
				list.Add(Populate(reader));
				}
				return list;
			}
		}

		/// <summary>
		/// Get DataSet of SubFunction
		/// </summary>
		/// <returns>DataSet</returns>
		public DataSet GetDataSet()
		{
			return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure,"sproc_SubFunction_Get");
		}


		/// <summary>
		/// Get all of SubFunction paged
		/// </summary>
		/// <param name="recperpage">record per page</param>
		/// <param name="pageindex">page index</param>
		/// <returns>List<<SubFunction>></returns>
		public List<SubFunction> GetListPaged(int recperpage, int pageindex)
		{
			using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_SubFunction_GetPaged"
							,Data.CreateParameter("recperpage", recperpage)
							,Data.CreateParameter("pageindex", pageindex)))
			{
				List<SubFunction> list = new List<SubFunction>();
				while (reader.Read())
				{
				list.Add(Populate(reader));
				}
				return list;
			}
		}

		/// <summary>
		/// Get DataSet of SubFunction paged
		/// </summary>
		/// <param name="recperpage">record per page</param>
		/// <param name="pageindex">page index</param>
		/// <returns>DataSet</returns>
		public DataSet GetDataSetPaged(int recperpage, int pageindex)
		{
			return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure,"sproc_SubFunction_GetPaged"
							,Data.CreateParameter("recperpage", recperpage)
							,Data.CreateParameter("pageindex", pageindex));
		}





		#endregion

		#region ***** Add Update Delete Methods ***** 
		/// <summary>
		/// Add a new SubFunction within SubFunction database table
		/// </summary>
		/// <param name="obj">SubFunction</param>
		/// <returns>key of table</returns>
		public int Add(SubFunction obj)
		{
			DbParameter parameterItemID = Data.CreateParameter("ID", obj.ID);
			parameterItemID.Direction = ParameterDirection.Output;
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_SubFunction_Add"
							,parameterItemID
							,Data.CreateParameter("FunctionID", obj.FunctionID)
							,Data.CreateParameter("SubFunctionName", obj.SubFunctionName)
							,Data.CreateParameter("Used", obj.Used)
			);
			return (int)parameterItemID.Value;
		}

		/// <summary>
		/// updates the specified SubFunction
		/// </summary>
		/// <param name="obj">SubFunction</param>
		/// <returns></returns>
		public void Update(SubFunction obj)
		{
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_SubFunction_Update"
							,Data.CreateParameter("ID", obj.ID)
							,Data.CreateParameter("FunctionID", obj.FunctionID)
							,Data.CreateParameter("SubFunctionName", obj.SubFunctionName)
							,Data.CreateParameter("Used", obj.Used)
			);
		}

		/// <summary>
		/// Delete the specified SubFunction
		/// </summary>
		/// <param name="id">ID</param>
		/// <returns></returns>
		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure,"sproc_SubFunction_Delete", Data.CreateParameter("ID", id));
		}
		#endregion
	}
}
